import {Result} from '@/base/Result';
import {request} from '@/base/Request';
import {Page} from '@/base/Page';
import {Affiliate} from '@/base/model/Affiliate';
import {TableSort} from '@/base/TableSort';
import {Member} from '@/base/model/Member';

export function getAffiliateList(params: {
  pageIndex: number,
  pageSize: number,
  memberId?: number,
  nickname?: string,
  createBegin?: string,
  createEnd?: string
}): Promise<Result<Page<Affiliate>>> {
  Object.assign(params, TableSort());
  return request.getPage(Affiliate, '/api/affiliate', {params});
}

export function getAffiliate(id: number): Promise<Result<Affiliate>> {
  return request.get(Affiliate, `/api/affiliate/${id}`);
}

export function putAffiliate(id: number, params: {
  birthday: string,
  height: number,
  nickname: string,
  phone: string,
  sex: string,
  weight: number
}): Promise<Result<Affiliate>> {
  let formData = new FormData();
  Object.keys(params).forEach(x => {
    formData.append(x, params[x]);
  });
  return request.put(Affiliate, `/api/affiliate/${id}`, formData);
}

export function postAffiliate(params: {
  birthday: string,
  height: number,
  nickname: string,
  sex: string,
  weight: number,
  memberId: number
}): Promise<Result<Affiliate>> {
  let formData = new FormData();
  Object.keys(params).forEach(x => {
    formData.append(x, params[x]);
  });
  return request.post(Affiliate, '/api/affiliate/root', formData);
}

export function deleteAffiliate(id: number): Promise<Result<void>> {
  return request.delete(`/api/affiliate/${id}`);
}
